﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;

namespace SuperQuery.UserControls.ToolBars
{
    /// <summary>
    /// An enumeration of possible toolbar buttons
    /// </summary>
    public enum ToolBarButtons
    {
        // Enums for the Database List Tool Bar
        AddDatabase = 0,
        RemoveDataBase = 1,
        SelectAll = 2,
        SelectFailures = 3,
        SelectSuccesses = 4,
        InvertSelection = 5,

        // Enums for the Query Toolbar
        RunAll = 6,
        RunSelected = 7,
        Stop = 8
    }

    /// <summary>
    /// Base class for the Tool Bar button controls
    /// </summary>
    public class ToolBarBase : UserControl
    {
        /// <summary>
        /// Delegate for the ToolBarOperation Event
        /// </summary>
        /// <param name="buttonPressed">The button that has been pressed</param>
        public delegate void ToolBarOperationEvent(ToolBarButtons buttonPressed);

        /// <summary>
        /// Event raised when a tool bar button is pressed
        /// </summary>
        public event ToolBarOperationEvent ToolBarOperation;

        /// <summary>
        /// Tool bar base constructor
        /// </summary>
        public ToolBarBase()
        {
            
            // Listen to when the Enabled property is changed and update the tool bar appropriately
            DependencyPropertyDescriptor enabledDescr = DependencyPropertyDescriptor.
                FromProperty(EnabledProperty, typeof(ToolBarBase));

            enabledDescr.AddValueChanged(this, EnabledChanged);
        }

        /// <summary>
        /// Enable or disable the buttons on this tool bar
        /// </summary>
        public bool Enabled
        {
            get
            {
                return (bool)GetValue(EnabledProperty);
            }
            set
            {
                SetValue(EnabledProperty, value);
            }
        }

        /// <summary>
        /// Event raised when the enabled property is changed
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        protected virtual void EnabledChanged(object sender, EventArgs e)
        {
            return;
        }

        /// <summary>
        /// This function is called by derived classed to fire the ToolBarOperation event if it is assigned to anything
        /// </summary>
        /// <param name="buttonPressed">The button that has been pressed</param>
        protected void OnToolBarOperation(ToolBarButtons buttonPressed)
        {
            if (ToolBarOperation != null)
            {
                ToolBarOperation(buttonPressed);
            }
        }

        // Using a DependencyProperty as the backing store for Enabled.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty EnabledProperty =
            DependencyProperty.Register("Enabled", typeof(bool), typeof(ToolBarBase), new UIPropertyMetadata(true));
    }
}
